package com.medical.seckill.scheduled;

import com.medical.seckill.service.SeckillService;
import com.medical.seckill.service.impl.SeckillServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
@Slf4j
public class SeckillScheduled {

    @Autowired
    private SeckillService seckillService;
    @Autowired
    private RedissonClient redissonClient;
    private final String LOCK="seckill:lock";

    /**
     * 定时查询需要上架的商品
     */
//    @Scheduled(cron = "*/10 * * * * *")
    public void uploadSeckillScheduled(){
        log.info("开始上架商品");
        RLock lock = redissonClient.getLock(LOCK);
        try {
            lock.lock(3, TimeUnit.SECONDS);
            seckillService.findUploadSeckill();
        }finally {
            lock.unlock();
        }
    }
}
